﻿using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace SuperSocialShare.Authorize
{
    /// <summary>
    /// 用户信息类
    /// </summary>
    [DataContract]
    public class UserInfo
    {
        /// <summary>
        /// 获取或设置用户姓名
        /// </summary>
        [DataMember]
        public string UserName { set; get; }

        /// <summary>
        /// 从网络返回的json数据中提取用户姓名
        /// </summary>
        /// <param name="jsondata">json数据</param>
        /// <param name="type">社区类型</param>
        internal void Fill(string jsondata, SocialType type)
        {
            string pattern = string.Empty;
            switch (type)
            {
                    //根据不同的社区用不同的正则表达式提取用户名
                case SocialType.Tencent:
                    pattern = @"(?<=""nick""[\s\S]*?:[\s\S]*?"").*?(?=""[\s\S]*?,)";
                    break;
                case SocialType.Sina:
                case SocialType.SoHu:
                case SocialType.NetEase:
                    pattern = @"(?<=""screen_name""[\s\S]*?:[\s\S]*?"").*?(?=""[\s\S]*?,)";
                    break;
                case SocialType.Renren:
                case SocialType.KaiXin:
                    pattern = @"(?<=""name""[\s\S]*?:[\s\S]*?"").*?(?=""[\s\S]*?,)";
                    break;
                default:
                    break;
            }
            if (!string.IsNullOrEmpty(pattern))
            {
                Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
                Match match = regex.Match(jsondata);
                if (match.Success)
                    UserName = match.Value;
            }
        }
    }
}
